<!--
  作者：xhr
  时间：2022年08月19日 16:40:10
-->
<template>
    <div style="padding: 10px">
        <div style="margin: 10px 0">
            <el-button type="primary" @click="add" v-if="user.role === 1">新增</el-button>
            <el-button type="primary">导入</el-button>
            <el-button type="primary">导出</el-button>
        </div>
        <div style="margin: 10px 0">
            <el-input v-model="search" placeholder="请输入内容" style="width: 20%" clearable></el-input>
            <el-button type="primary" style="margin-left: 10px" @click="loading">查询</el-button>
        </div>
        <el-table :data="tableData" border stripe>
            <el-table-column sortable prop="id" label="ID"></el-table-column>
            <el-table-column prop="name" label="书名"></el-table-column>
            <el-table-column prop="price" label="价格"></el-table-column>
            <el-table-column prop="author" label="作者"></el-table-column>
            <el-table-column prop="createTime" label="出版时间"></el-table-column>
            <el-table-column label="封面">
                <template slot-scope="scope">
                    <el-image style="width: 100px; height: 100px" :src="scope.row.cover"
                        :preview-src-list="[scope.row.cover]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                    <el-button @click="update(scope.row)" size="mini">编辑 </el-button>
                    <el-popconfirm title="确定删除吗？" style="margin-left: 5px" @confirm="delate(scope.row.id)">
                        <el-button type="danger" size="mini" slot="reference">删除
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="currentPage" :page-sizes="[5, 10, 50]" :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
            <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
                <el-form label-width="120px" :model="form">
                    <el-form-item label="书名">
                        <el-input v-model="form.name" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="价格">
                        <el-input v-model="form.price" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="作者">
                        <el-input v-model="form.author" style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="出版时间">
                        <!-- <el-date-picker v-model="form.createTime" clearable type="date" value-format="YYYY-MM-DD" style="width: 80%"></el-date-picker> -->
                    </el-form-item>
                    <el-form-item label="封面">
                        <!-- <el-date-picker v-model="form.createTime" clearable type="date" value-format="YYYY-MM-DD" style="width: 80%"></el-date-picker> -->
                        <el-upload ref="upload" action="http://localhost:9090/files/upload"
                            :on-success="filesUploadSuccess">
                            <el-button type="primary">点击上传</el-button>
                        </el-upload>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="save">确 定</el-button>
                </span>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import request from "@/utils/request";

export default {
    name: "Book",
    data() {
        return {
            user:{},
            form: {

            },
            total: 0,
            currentPage: 1,
            pageSize: 10,
            search: "",
            dialogVisible: false,
            tableData: [],
        };
    },
    created() {
        this.loading();

        let userStr = sessionStorage.getItem("user") || "{}"
        this.user = JSON.parse(userStr)
        request.get("/user/" + this.user.id).then(res => {
            if (res.code === '0') {
                this.user = res.data
            }
        }

        )

    },
    methods: {
        filesUploadSuccess(res) {
            this.form.cover = res.data

        },
        loading() {
            request
                .get("/book", {
                    params: {
                        pagenum: this.currentPage,
                        pagesize: this.pageSize,
                        search: this.search,
                    },
                })
                .then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                });
        },
        save() {
            if (this.form.id) {
                request.put("/book", this.form).then((res) => {
                    console.log(res);
                    if (res.code === "0") {
                        this.$message({
                            type: "success",
                            message: "更新成功",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: "更新失败",
                        });
                    }
                    this.loading();
                    this.dialogVisible = false;
                });
            } else {
                request.post("/book", this.form).then((res) => {
                    console.log(res);
                    if (res.code === "0") {
                        this.$message({
                            type: "success",
                            message: "新增成功",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: "新增失败",
                        });
                    }
                    this.loading();
                    this.dialogVisible = false;
                });
            }
        },
        add() {
            this.dialogVisible = true;
            this.form = {};
            this.$nextTick(() => {
                this.$refs.upload.clearFiles();
            })
           

        },
        update(row) {
            this.form = JSON.parse(JSON.stringify(row));
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.$refs.upload.clearFiles();
            })

        },

        delate(id) {
            request.delete("/book/" + id).then((res) => {
                if (res.code === "0") {
                    this.$message({
                        type: "success",
                        message: "删除成功",
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "删除失败",
                    });
                }
            });
            this.loading();
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loading();
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum;
            this.loading();
        },
    },
};
</script>

<style>
</style>